<template>
  <div id="homeView">
    <el-card shadow="always">

      <el-form :inline="true" size="medium" :model="employeeSearch" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="employeeSearch.employeeName" placeholder="员工姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="employeeSearch.employeeTitle" placeholder="员工职务"></el-input>
        </el-form-item>
        <el-form-item >
          <el-select v-model="employeeSearch.employeeSex" placeholder="员工性别">
            <el-option label="所有员工" value=""></el-option>
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  plain @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="editemployee" icon="el-icon-plus">添加员工</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" icon="el-icon-delete" plain @click="deleteemployee" >批量删除</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"   plain @click="login">进入库存信息管理</el-button>
        </el-form-item>
      </el-form>

      <el-table
          :data="employeeList"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            label="员工编号"
            prop="employeeId"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="员工姓名"
            align="center"
            prop="employeeName">
        </el-table-column>
        <el-table-column
            label="员工性别"
            align="center"
            prop="employeeSex">
        </el-table-column>
        <el-table-column
            label=" 员工年龄"
            align="center"
            prop="employeeAge">
        </el-table-column>
        <el-table-column
            label="员工职务"
            align="center"
            prop="employeeTitle">
        </el-table-column>

        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
                size="mini"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deleteemployee(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>

    </el-card>

    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">

      <el-form :model="employee" :rules="employeerules" ref="ruleForm">
        <el-form-item label="员工姓名" prop="employeeName" :label-width="formLabelWidth">
          <el-input v-model="employee.employeeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工性别" prop="sex" :label-width="formLabelWidth">
          <el-radio v-model="employee.employeeSex" label="男">男</el-radio>
          <el-radio v-model="employee.employeeSex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="员工年龄" prop="employeeAge" :label-width="formLabelWidth">
          <el-input v-model.number="employee.employeeAge" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label=" 员工职务" prop="employeeTitle" :label-width="formLabelWidth">
          <el-input v-model="employee.employeeTitle" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveemployee">确 定</el-button>
      </div>
    </el-dialog>




  </div>
</template>

<script>
import employeeApi from "@/api/employee"

export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (value===''||value.length==0) {
        return callback(new Error('年龄不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback();
        } else {
          if (value > 200|| value<1) {
            callback(new Error('年龄在1岁到200岁之间'));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    return {
      employeeList: [],
      page:1,
      pageSize:5,
      total:100,
      employeeSearch:{},


      dialogFormVisible:false,
      sids:[],
      multipleSelection: [],
      formLabelWidth:'100px',
      employee: {},
      employeerules: {
        employeeName: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },

        ],
        employeeSex: [
          { required: true, message: '请选择员工性别', trigger: 'change' }
        ],
        employeeAge: [
          { validator: checkAge, trigger: 'blur' }
        ],
        employeeTitle: [
          { required: true, message: '请输入员工职务', trigger: 'blur'  }
        ]

      },
      title: '添加员工',

    }
  },
  created() {


    this.search1()

  },

  methods: {



    async search(){
      const {data:res} = await employeeApi.search(1,this.pageSize,this.employeeSearch)
      console.log(res)
      if (res.code === 200) {
        this.employeeList = res.data.pr.rows
        this.total = res.data.pr.total
      }
      this.page=1
    },
    async search1(){
      const {data:res} = await employeeApi.search(this.page,this.pageSize,this.employeeSearch)
      console.log(res)
      if (res.code === 200) {
        this.employeeList = res.data.pr.rows
        this.total = res.data.pr.total
      }
    },

    editemployee(){
      this.title='添加员工';
      this.employee={};
      this.dialogFormVisible = true
    },

    async saveemployee(){
      this.$refs.ruleForm.validate(async valid => {
        let url = "/sEmployee/save"
        let method = "post"
        if(this.employee.employeeId){
          url = "/sEmployee/update"
          method = "put"}


        if (valid) {
          const {data:res} = await employeeApi.save(url,method,this.employee)

          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: res.message,
              duration:1000,
              onClose: () => {

                this.dialogFormVisible = false

                this.search1()
              }
            })
          }else{
            this.$message({
              type: 'error',
              message: res.message,
              duration:1000
            })
          }

          console.log('验证成功');
        } else {
          console.log('验证失败');
          return false;
        }
      });

    },
    async deleteemployee(index, row){
      this.sids = []
      if(row){
        this.sids.push(row.employeeId)
      }else{
        this.sids = this.multipleSelection.map(m => m.employeeId)
      }
      if(this.sids.length==0)
      {
        this.$message({
          type: 'warning',
          message: '没有选择要删除的员工',
          duration: 1000
        });
        return;

      }
      this.$confirm(`是否真的要删除员工编号为${this.sids}的员工吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let _this = this
        employeeApi.delete(this.sids)
            .then(({data:res}) => {
              console.log("res:",res)
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: res.message,
                  duration:1000,
                  onClose: () => {

                    _this.search1()
                  }
                })
              } else {
                this.$message({
                  type: 'error',
                  message: res.message,
                  duration:1000
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleEdit(index, row) {

      this.title='修改员工信息';
      this.employee=row;
      console.log(this.student);
      this.dialogFormVisible=true;


    },
    login(){
      this.$router.push('/aboutview');
    },


    // 每页大小改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;

      this.search1()
    },
    // 当前页改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;

      this.search1()
    },
    // 选择学生改变
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
}
</script>
<style>
.el-card{
  width: 90%;
//margin: 0 auto;
}
#app{
  display: flex;
  justify-content: center;
}
.el-pagination{
  text-align: right;
  margin-top: 15px;
}
</style>
